mini.parse();

function MdiHandler() {
};

MdiHandler.instance = new MdiHandler();

MdiHandler.isQueryBtnExpended = false;

//获得全局的会员资料项数据表格对象
MdiHandler.mdiGrid = mini.get("mdiGrid");

//定义会员资料列表页功能权限字符编码数组
MdiHandler.purArrayMdi = new Array("sys_mitem_add", "sys_mitem_edit", "sys_mitem_del", "sys_mitem_refresh");

/**
 * 功能按钮权限控制及其展示
 */
MdiHandler.loadPurviewBtn = function() {
	var userArr = MiniuiTools.isHasPurview(MdiHandler.purArrayMdi);
	
	for (var i = 0; i < userArr.length; i++) {
		var purviewBtn = mini.get(MdiHandler.purArrayMdi[i]);
		
		if(purviewBtn){
			purviewBtn.setVisible(userArr[i]);
		}
	}
};

/**
 * 初始化
 */
MdiHandler.init = function(){
	//加载权限功能按钮
	MdiHandler.loadPurviewBtn();
	
	//加载表格数据
	MdiHandler.mdiGrid.load();
}

MdiHandler.init();

/**
 * 弹出添加会员资料项窗口
 */
MdiHandler.addMdi = function() {
	miniWin.afreshNewWin();
	miniWin.setParams(354, 317, "添加会员资料项");
	miniWin.setOption("bodyStyle","padding:0px;");

	miniWin.load("memberDataItemForm.jsp",function(){
		var iframe = miniWin.getIFrameEl();
        var data = {action: "new"};
        iframe.contentWindow.MdifHandler.passData(data);
	},function(action){
		MdiHandler.mdiGrid.reload();
	});
	
	miniWin.show();
};

/**
 * 弹出编辑会员资料项窗口
 */
MdiHandler.editMdi = function() {
	var rowData = MdiHandler.mdiGrid.getSelected();
	var rowsData = MdiHandler.mdiGrid.getSelecteds();
	
	if(rowsData.length == 1){
		miniWin.afreshNewWin();
		miniWin.setParams(354, 317, "编辑会员资料项[<font color='blue'>"	+ rowData.biiName + "</font>]");
		miniWin.setOption("bodyStyle","padding:0px;");
		
		miniWin.load("memberDataItemForm.jsp",function(){
			var iframe = miniWin.getIFrameEl();
            var data = {action: "edit", biiId: rowData.biiId};
            iframe.contentWindow.MdifHandler.passData(data);
		},function(action){
			MdiHandler.mdiGrid.reload();
		});
		
		miniWin.show();
	}else if (rowsData.length > 1){
		MiniuiTools.miniWarning("系统提示","不能同时编辑多条资源！");
	}else {
		MiniuiTools.miniWarning("系统提示","请选择要编辑的资源！");
	}
	
};

/**
 * 删除会员资料项资源
 */
MdiHandler.delMdi = function() {
	var rowsData = MdiHandler.mdiGrid.getSelecteds();
	
	if(rowsData.length > 0){
		mini.confirm("确认删除选择的" + rowsData.length + "条记录？" ,"系统提示",
			function(action, value){
				if (action == "ok") {
					var ids = "";
					
					for ( var i = 0; i < rowsData.length - 1; i++){
						ids += rowsData[i].biiId+ ",";
					}
					ids += rowsData[rowsData.length - 1].biiId;
					
					// 判断选择的资料项是否已被会员使用
					if(MdiHandler.getBmiBybii(ids)){
						MiniuiTools.miniWarning("系统提示", "选择的记录存在会员资料信息关联，请检查后重试！");
						return;	
					}
					
					MdiHandler.mdiGrid.loading();
					
                    $.ajax({
                        url: __root + "/delMdiByIds.do",
                        data: {
				        	biiIds: ids,
				        	lslUser: gbOperateUser,
				    		lslAddress: gbOperateIp
				        },
                        success: function (text) {
                        	MiniuiTools.miniInfo("系统提示","操作成功！", function(action){
				        		MdiHandler.mdiGrid.reload();
				        	});
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                        	MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
                        },
                        complete: function(){
                        	MdiHandler.mdiGrid.unmask();
                        }
                    });	
                } 
			}
		)
	}else{
		MiniuiTools.miniWarning("系统提示","请选择要删除的资源！");
	}
};

/**
 * 判断选择的资料项是否已被会员使用
 * @param {} baiIds
 * @return {}
 */
MdiHandler.getBmiBybii = function(biiIds){
	var isUsed = false;
	
	$.ajax({
        url: __root + "/getBmiBybii.do?biiIds=" + biiIds,
        dataType: 'json',
        async: false,   // 同步执行，默认为true
        success: function (text) {
        	if(text == "1"){
        		isUsed = true;
        	}
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
        }
    });
    
    return isUsed;
}

/**
 * 更新会员资料项状态
 */
MdiHandler.updateMdiState = function() {
	if (!MiniuiTools.hasPurview("sys_mitem_edit")) {
		return;
	}
	
	var rowData = MdiHandler.mdiGrid.getSelected();
	var biiId = rowData.biiId;	
	MdiHandler.mdiGrid.loading();
	
    $.ajax({
        url: __root + "/updateMdiStateById.do",
        data: {
        	biiId: biiId,
        	lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
        },
        success: function (text) {
            MdiHandler.mdiGrid.reload();
        },
        error: function (jqXHR, textStatus, errorThrown) {
            MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
        },
        complete: function(){
        	MdiHandler.mdiGrid.unmask();
        }
    });	
};

/**
 * 更新会员资料项 注册选项 状态
 */
MdiHandler.updateRegState = function() {
	if (!MiniuiTools.hasPurview("sys_mitem_edit")) {
		return;
	}
	
	var rowData = MdiHandler.mdiGrid.getSelected();
	var biiId = rowData.biiId;	
	MdiHandler.mdiGrid.loading();
	
    $.ajax({
        url: __root + "/updateRegStateById.do",
        data: {
        	biiId: biiId,
        	lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
        },
        success: function (text) {
            MdiHandler.mdiGrid.reload();
        },
        error: function (jqXHR, textStatus, errorThrown) {
            MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
        },
        complete: function(){
        	MdiHandler.mdiGrid.unmask();
        }
    });	
};

/**
 * 刷新页面数据及布局
 */
MdiHandler.afresh = function(){
	MdiHandler.mdiGrid.reload();
}

/**
 * DrawCell 绘制单元格
 */
MdiHandler.mdiGrid.on("drawcell", function(e) {
	 var record = e.record;
     var column = e.column;
     var field = e.field;
     var value = e.value;
     
     if(column.field == "biiType"){
     	if(e.value == 0){
     		e.cellHtml = "文本框";
     	}else if(e.value == 1){
     		e.cellHtml = "文本域";
     	}else if(e.value == 2){
     		e.cellHtml = "下拉选择框";
     	}else if(e.value == 3){
     		e.cellHtml = "单选";
     	}else if(e.value == 4){
     		e.cellHtml = "复选";     	
     	}else if(e.value == 5){
     		e.cellHtml = "时间";   
     	}else{
     		e.cellHtml = "";   
     	}
     }

     if (column.field == "biiState") {
        if (e.value == 1) {
            e.cellHtml = "<span class='icon-show-yes' style='cursor: pointer;' onclick='MdiHandler.updateMdiState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";                    
        } else {
            e.cellHtml = "<span class='icon-show-no' style='cursor: pointer;' onclick='MdiHandler.updateMdiState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
        }
    }
    
    if (column.field == "biiIsReg") {
        if (e.value == 1) {
            e.cellHtml = "<span class='icon-show-yes' style='cursor: pointer;' onclick='MdiHandler.updateRegState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";                    
        } else {
            e.cellHtml = "<span class='icon-show-no' style='cursor: pointer;' onclick='MdiHandler.updateRegState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
        }
    }
});

